package com.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ydd.entity.Menu;
import com.ydd.entity.SysUser;
import com.ydd.mapper.MenuMapper;
import com.ydd.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.vo.SysMenuAllDateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author hgz
 * @since 2023-05-30
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;


    /**
     * 新增数据
     *
     * @param menu
     */
    @Override
    public void insertSysMenu (Menu menu){
        menu.setIsShow(1);
        //补全数据
        menu.setCreateDate(LocalDateTime.now());
        menu.setUpdateDate(LocalDateTime.now());


        //调用数据类新增数据
        menuMapper.insert(menu);

    }


    /**
     *菜单详情
     * @param id
     * @return
     */
    @Override
    public Menu rowInfo(Integer id) {
        return menuMapper.rowInfo(id);
    }


    /**
     * 菜单删除
     *
     * @param id
     */
    @Override
    public void deleteBySysMenuId(String id) {
        ////封装删除条件
        //SysMenu sysMenu = new SysMenu();
        //sysMenu.setId(Integer.valueOf(id));
        //
        ////调用数据类修改删除字段，进行逻辑删
        //sysMenuMapper.deleteById(sysMenu);
        //// 更新缓存
        //updateCache(sysMenu.getPid());

        SysUser sysUser = new SysUser();
        sysUser.setId(String.valueOf(id));

        menuMapper.deleteById(sysUser);

    }


    /**
     * 菜单修改
     * @param menu
     */
    @Override
    public void Menuupdate(Menu menu) {

        menuMapper.MenuUpdate(menu);
    }



    /**
     * 所有菜单
     * @return
     */
    @Override
    public List<SysMenuAllDateVO> selectAllSysMenu() {
        //定义id为0的父VO进行查询pid为0的父模块
        SysMenuAllDateVO temp = new SysMenuAllDateVO();
        temp.setId(0);
        List<SysMenuAllDateVO> fatherList = Arrays.asList(temp);
        //调用递归方法
        setSon(fatherList);
        //将定义的0父VO的Children返回
        List<SysMenuAllDateVO> SysMenuAllDataVOFather = fatherList.get(0).getChildren();
        //返回数据
        return SysMenuAllDataVOFather;
    }


    /**
     * 所有菜单（递归查询方法）
     * @param fatherList
     */
    private void setSon(List<SysMenuAllDateVO> fatherList) {
        //遍历查询id = pid 的数据
        fatherList.forEach(father -> {
            List<SysMenuAllDateVO> sysMenuAllDataVOList = new ArrayList<>();
            List<Menu> SysMenuList = menuMapper.selectList(new LambdaQueryWrapper<Menu>()
                    .eq(Menu::getPid, father.getId()));

            //遍历封装数据返回
            for (Menu sysMenu : SysMenuList) {
                SysMenuAllDateVO sysMenuAllDataVO = new SysMenuAllDateVO();
                BeanUtils.copyProperties(sysMenu,sysMenuAllDataVO);
                sysMenuAllDataVOList.add(sysMenuAllDataVO);
            }
            //调用方法递归
            setSon(sysMenuAllDataVOList);
            //设置0父VO的Children属性
            father.setChildren(sysMenuAllDataVOList);
        });
    }
}
